package fr.esiea.es_inf5042.persistance.dao.SQLiteDAO;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import fr.esiea.es_inf5042.business.Comment;
import fr.esiea.es_inf5042.business.User;
import fr.esiea.es_inf5042.persistance.dao.AbstractDAOFactory;
import fr.esiea.es_inf5042.persistance.dao.DAO;

public class SQLiteCommentDAO extends DAO<Comment>{

	private SQLiteDatabase database;
	
	public SQLiteCommentDAO(SQLiteDatabase database) {
			
		this.database = database;
	}
	
	@Override
	public List<Comment> getAll(String whereClauses) {
		
		AbstractDAOFactory daoFactory = null;
		DAO<User> userDAO = null;		
		List<Comment> resultList = null;
		Comment c = null;
		Cursor cursor = null;
		String sql = null;		
		
		daoFactory = AbstractDAOFactory.getDAOFactory(SQLiteDAOFactory.getContext());
		userDAO = daoFactory.getUserDAO();		
		resultList = new ArrayList<Comment>();
		
		//KSE - Get all comments from database.
		sql = "SELECT * FROM " + TABLE_COMMENTS;
		if(whereClauses != null) sql = sql + " WHERE " + whereClauses;
		
		cursor = database.rawQuery(sql, null);
		
		if(cursor.moveToFirst())
		{
			do
			{
				c = new Comment(
									cursor.getInt(0),
									userDAO.find(cursor.getInt(1)), 
									cursor.getString(2),
									cursor.getInt(3)
								);
				
				resultList.add(c);
			}
			while(cursor.moveToNext());
		}
		
		return resultList;
	}

	@Override
	public Comment find(int id) {

		AbstractDAOFactory daoFactory = null;
		DAO<User> userDAO = null;		
		Comment c = null;
		Cursor cursor = null;
		String sql = null;		
		
		daoFactory = AbstractDAOFactory.getDAOFactory(SQLiteDAOFactory.getContext());
		userDAO = daoFactory.getUserDAO();		
		
		//KSE - GEt comment by its id. 
		sql = "SELECT * FROM " + TABLE_COMMENTS + " WHERE id = " + String.valueOf(id);
		
		cursor = database.rawQuery(sql, null);
		
		if(cursor.moveToFirst())
		{
			c = new Comment(
								cursor.getInt(0),
								userDAO.find(cursor.getInt(1)), 
								cursor.getString(2),
								cursor.getInt(3)
							);
		}
		
		database.close();
		return c;
	}

	@Override
	public Comment create(Comment obj) {

		Comment c = null;
		Cursor cursor = null;
		String sql = null;
		
		//KSE - Insert new row into table.
		sql = "INSERT INTO " + TABLE_COMMENTS + " VALUES(" +
															String.valueOf(obj.getId()) + ", " +
															String.valueOf(obj.getUser().getId()) + ", " +
															"\'" + obj.getText() + "\'" + ", " +
															String.valueOf(obj.getBargainID()) +
														")";
		
		database.rawQuery(sql, null);
				
		//KSE - Get the current entry from the database.
		sql = "SELECT id FROM " + TABLE_COMMENTS + " WHERE id = last_insert_rowid()";		
		cursor = database.rawQuery(sql, null);		
		if(cursor.moveToFirst())
			c = this.find(cursor.getInt(0));
				
		database.close();
		return c;
	}

	@Override
	public void update(Comment obj) {

		String sql = null;
		
		// KSE - Update the attribute object "obj" into database.
		sql = "UPDATE " + TABLE_COMMENTS + " SET " +													
													"fk_user = " + String.valueOf(obj.getUser().getId()) + ", " +
													"content = " + "\'" + obj.getText() + "\'" + ", " +
													"fk_bargain = " + String.valueOf(obj.getBargainID()) +
											" WHERE id = " + String.valueOf(obj.getId());
		
		database.rawQuery(sql, null);
		
		database.close();
	}

	@Override
	public void delete(Comment obj) {

		String sql = null;
		
		//KSE - Delete the attribute object "obj" into database.
		sql = "DELETE FROM " + TABLE_COMMENTS + " WHERE id = " + String.valueOf(obj.getId());
		
		database.rawQuery(sql, null);
		
		database.close();
	}
}
